<?php
namespace app\index\controller;

use app\common\controller\Common;
use org\Page;
use think\Exception;
use think\facade\Cache;
use think\facade\Config;
use think\facade\Request;

class News extends Common
{

    protected function initialize()
    {
        parent::initialize();
    }

    public function detail($id = 0) {

        //资讯详情
        $detail = curl_server(sprintf("apiv2/newsdetail?id=%s",$id));
        //随机资讯
        $rand_news = curl_server(sprintf("apiv2/randnews?limit=10&flag=" . $id));
        //热门资讯
        $hot_news = curl_server(sprintf("apiv2/newslist?cid=%s&page=%d&page_size=%d&hot=true",0,1,10));
        //热门专辑
        $hot_tags_list = curl_server(sprintf("apiv2/speciallist?hot=true&page=%d&page_size=%d",1,4));
        $hot_tags_list = $hot_tags_list['data']['list'];

        $detail = $detail['data'];
        $rand_news = $rand_news['data'];
        $hot_news = $hot_news['data'];

        $this->site_seo('news_detail',array("title" => $detail['title'],"chapter_title" => $detail['chapter_title'],"introduction" => $detail['introduction']));
        return $this->fetch('detail',['detail' => $detail,'rand_news' => $rand_news,'hot_news' => $hot_news['list'],'hot_tags_list' => $hot_tags_list]);
    }

    public function index($page = 1)
    {
        $page_size = 15; //页码大小
        $data = curl_server(sprintf("apiv2/newslist?cid=%s&page=%d&page_size=%d",0,$page,$page_size));
        $data = $data['data'];

        //热门资讯
        $hot_news = curl_server(sprintf("apiv2/newslist?cid=%d&page=%d&page_size=%d&hot=true",0,1,12));
        $hot_new_list = $hot_news['data']['list'];

        //热门专辑
        $hot_tags_list = curl_server(sprintf("apiv2/speciallist?hot=true&page=%d&page_size=%d",1,4));
        $hot_tags_list = $hot_tags_list['data']['list'];

        //分页
        $pages = new Page("news/list_{PAGE}",$page,$data['total'],$page_size);
        if (isMobileDomain()) {
            $pages->setConfig("theme",'%NO_PREV% %UP_PAGE% %DOWN_PAGE%');
            $show = $pages->showHs13Mobile();
        }else {
            $show = $pages->show();
        }

        $this->assign("pages",$show);

        $this->site_seo('news_list');
        return $this->fetch('index',['total' => $data['total'],'list' => $data['list'],'hot_new_list' => $hot_new_list,'hot_tags_list' => $hot_tags_list]);
    }
}

